package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ErpWorkProcedure;
import com.nuoniu.sibanyun.mapper.erp.ErpProcessRouteMapper;
import com.nuoniu.sibanyun.mapper.erp.ErpWorkProcedureMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpProcessRouteService;
import com.nuoniu.sibanyun.service.erp.IErpTeamService;
import com.nuoniu.sibanyun.service.erp.IErpWorkProcedureService;
import com.nuoniu.sibanyun.service.production.IErpStationService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 工序管理 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-30
 */
@Service
public class ErpWorkProcedureServiceImpl extends ServiceImpl<ErpWorkProcedureMapper, ErpWorkProcedure> implements IErpWorkProcedureService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IErpTeamService teamService;
    @Autowired
    private IErpStationService stationService;
    @Autowired
    private ErpProcessRouteMapper processRouteMapper;

    @Override
    public Page<ErpWorkProcedure> pageList(Page pages, ErpWorkProcedure workProcedure) {
        QueryWrapper<ErpWorkProcedure> queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        if(StringUtils.isNotBlank(workProcedure.getSearchKey())) {
            queryWrapper.and(wrapper -> wrapper.likeRight("code", workProcedure.getSearchKey()).or().like("name", workProcedure.getSearchKey()));
        }

        return page(pages, queryWrapper);
    }

    @Override
    public void saveWorkProcedure(ErpWorkProcedure workProcedure) {
        UserInfo userInfo = UserThreadLocal.get();
        ErpWorkProcedure procedureByCode = baseMapper.getByCode(workProcedure.getCode(), userInfo.getCompanyId());
        if(procedureByCode != null && (workProcedure.getId() == null || !workProcedure.getId().equals(procedureByCode.getId()))) {
            throw new NuoNiuException("编码不能重复");
        }

        ErpWorkProcedure procedureByName = baseMapper.getByName(workProcedure.getName(), userInfo.getCompanyId());
        if(procedureByName != null && (workProcedure.getId() == null || !workProcedure.getId().equals(procedureByName.getId()))) {
            throw new NuoNiuException("名称不能重复");
        }

        if(workProcedure.getId() == null) {
            workProcedure.setCompanyId(userInfo.getCompanyId());
            workProcedure.setAccountId(accountService.getCurrentAccountId());
        }else {
            Integer count = processRouteMapper.getByWP(workProcedure.getId());
            if(count > 0) {
                throw new NuoNiuException("已使用，不能修改");
            }

            workProcedure.setUpdateDate(LocalDateTime.now());
        }

        if(workProcedure.getTeamId() != null) {
            workProcedure.setTeamName(teamService.getById(workProcedure.getTeamId()).getName());
        }

        if(workProcedure.getStationId() != null) {
            workProcedure.setStationName(stationService.getById(workProcedure.getStationId()).getName());
        }

        this.saveOrUpdate(workProcedure);
    }

    @Override
    public void deleteById(Long id) {
        Integer count = processRouteMapper.getByWP(id);
        if(count > 0) {
            throw new NuoNiuException("已使用，不能删除");
        }

        removeById(id);
    }
}
